package com.homepage.service;

import com.homepage.domain.Headline;
import com.homepage.repository.HeadlineRepository;
import com.homepage.service.dto.HeadlineDTO;
import com.homepage.service.mapper.HeadlineMapper;
import java.util.LinkedList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * Service Implementation for managing {@link Headline}.
 */
@Service
@Transactional
public class HeadlineService {

    private final Logger log = LoggerFactory.getLogger(HeadlineService.class);

    private final HeadlineRepository headlineRepository;

    private final HeadlineMapper headlineMapper;

    public HeadlineService(HeadlineRepository headlineRepository, HeadlineMapper headlineMapper) {
        this.headlineRepository = headlineRepository;
        this.headlineMapper = headlineMapper;
    }

    /**
     * Save a headline.
     *
     * @param headlineDTO the entity to save.
     * @return the persisted entity.
     */
    public HeadlineDTO save(HeadlineDTO headlineDTO) {
        log.debug("Request to save Headline : {}", headlineDTO);
        Headline headline = headlineMapper.toEntity(headlineDTO);
        headline = headlineRepository.save(headline);
        return headlineMapper.toDto(headline);
    }

    public List<HeadlineDTO> saveAll(List<HeadlineDTO> headlineDTOS) {
        log.debug("Request to save Headline : {}", headlineDTOS);
        List<Headline> headline = headlineMapper.toEntity(headlineDTOS);
        return headlineRepository.saveAll(headline).stream()
                .map(headlineMapper::toDto).collect(Collectors.toCollection(LinkedList::new));
    }

    /**
     * Partially update a headline.
     *
     * @param headlineDTO the entity to update partially.
     * @return the persisted entity.
     */
    public Optional<HeadlineDTO> partialUpdate(HeadlineDTO headlineDTO) {
        log.debug("Request to partially update Headline : {}", headlineDTO);

        return headlineRepository
            .findById(headlineDTO.getId())
            .map(existingHeadline -> {
                headlineMapper.partialUpdate(existingHeadline, headlineDTO);

                return existingHeadline;
            })
            .map(headlineRepository::save)
            .map(headlineMapper::toDto);
    }

    /**
     * Get all the headlines.
     *
     * @return the list of entities.
     */
    @Transactional(readOnly = true)
    public List<HeadlineDTO> findAll() {
        log.debug("Request to get all Headlines");
        return headlineRepository.findAll().stream().map(headlineMapper::toDto).collect(Collectors.toCollection(LinkedList::new));
    }

    @Transactional(readOnly = true)
    public List<HeadlineDTO> findAllByCompanyId(String companyName) {
        log.debug("Request to get all Headlines by " + companyName);
        return headlineRepository.findAllByCompanyId(companyName).stream()
                .map(headlineMapper::toDto).collect(Collectors.toCollection(LinkedList::new));
    }

    /**
     *  Get all the headlines where HeadlineImage is {@code null}.
     *  @return the list of entities.
     */
    @Transactional(readOnly = true)
    public List<HeadlineDTO> findAllWhereHeadlineImageIsNull() {
        log.debug("Request to get all headlines where HeadlineImage is null");
        return StreamSupport
            .stream(headlineRepository.findAll().spliterator(), false)
            .map(headlineMapper::toDto)
            .collect(Collectors.toCollection(LinkedList::new));
    }

    /**
     * Get one headline by id.
     *
     * @param id the id of the entity.
     * @return the entity.
     */
    @Transactional(readOnly = true)
    public Optional<HeadlineDTO> findOne(Long id) {
        log.debug("Request to get Headline : {}", id);
        return headlineRepository.findById(id).map(headlineMapper::toDto);
    }

    /**
     * Delete the headline by id.
     *
     * @param id the id of the entity.
     */
    public void delete(Long id) {
        log.debug("Request to delete Headline : {}", id);
        headlineRepository.deleteById(id);
    }
}
